/**
 * Filename: ExStaggeredGridLayoutManager.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/4/18 15:01
 */
package com.github.postsanf.yinian.ynlist;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

public class ExStaggeredGridLayoutManager extends StaggeredGridLayoutManager {

	private final String TAG = getClass().getSimpleName();

	GridLayoutManager.SpanSizeLookup mSpanSizeLookup;

	public ExStaggeredGridLayoutManager(int spanCount, int orientation) {
		super(spanCount, orientation);
	}

	public GridLayoutManager.SpanSizeLookup getSpanSizeLookup() {
		return mSpanSizeLookup;
	}

	/**
	 * 设置某个位置的item的跨列程度，这里和GridLayoutManager有点不一样，
	 * 如果你设置某个位置的item的span>1了，那么这个item会占据所有列
	 */
	public void setSpanSizeLookup(GridLayoutManager.SpanSizeLookup spanSizeLookup) {
		mSpanSizeLookup = spanSizeLookup;
	}

	@Override
	public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
		//Log.d(TAG, "item count = " + getItemCount());
		for (int i = 0; i < getItemCount(); i++) {

			if (mSpanSizeLookup.getSpanSize(i) > 1) {
				//Log.d(TAG, "lookup > 1 = " + i);
				try {
					//fix 动态添加时报IndexOutOfBoundsException
					View view = recycler.getViewForPosition(i);
					if (view != null) {
						// 占用所有的列
						StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
						lp.setFullSpan(true);
					}
					// recycler.recycleView(view);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		super.onMeasure(recycler, state, widthSpec, heightSpec);
	}
}